package com.easytodo;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.androidnetworking.error.ANError;
import com.androidnetworking.interfaces.StringRequestListener;
import com.easytodo.entity.Item;
import com.easytodo.ui.today.TodayFragment;
import com.easytodo.ui.todo.TodoFragment;

import java.util.Date;
import java.util.List;

public class ItemAdapter extends BaseAdapter {
    Context context;
    List<Item> itemList;
    LayoutInflater inflater;
    Fragment fragment;
    String who;
    StringRequestListener listener;
    AppCompatActivity activity;

    public final class ItemView{
        TextView title;
        TextView last;
        TextView need;
        RelativeLayout delete;
        RelativeLayout finish;
        RelativeLayout bak;
        RelativeLayout bak1;
    }

    public ItemAdapter(Context context, List<Item> itemList, Fragment fragment, String who, StringRequestListener listener, AppCompatActivity activity){
        this.context=context;
        this.itemList=itemList;
        inflater=LayoutInflater.from(context);
        this.fragment=fragment;
        this.who=who;
        this.listener=listener;
        this.activity=activity;

    }
    public ItemAdapter(Context context, List<Item> itemList, Fragment fragment, String who){
        this.context=context;
        this.itemList=itemList;
        inflater=LayoutInflater.from(context);
        this.fragment=fragment;
        this.who=who;
    }
    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public Object getItem(int i) {
        return itemList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ItemView itemView=null;


        if(view==null){
            itemView=new ItemView();
            view=inflater.inflate(R.layout.item,null,false);
            itemView.title=view.findViewById(R.id.itemTitle);
            itemView.last=view.findViewById(R.id.last);
            itemView.need=view.findViewById(R.id.needed);
            itemView.delete=view.findViewById(R.id.deleteItem);
            itemView.finish=view.findViewById(R.id.finish);
            itemView.bak=view.findViewById(R.id.bak);
            itemView.bak1=view.findViewById(R.id.bak1);

            view.setTag(itemView);
        }else {
            itemView = (ItemView) view.getTag();
        }
        itemView.title.setText(itemList.get(i).getTitle());

        Date now=new Date();
        Long left=itemList.get(i).getDeathLine()-now.getTime();
        Double leftTime=Math.abs(left)/(1000.0*60*60);

        itemView.last.setText(String.format("%.1fh  left",leftTime));
        itemView.need.setText(String.format("%dmin needed",itemList.get(i).getNeedTime()/(1000*60)));

        if(left<0)
            itemView.last.setText("Deadline is out");

        String importance=itemList.get(i).getImportance();

        if(importance.equals("0")) {
            itemView.bak1.setBackgroundColor(Color.parseColor("#bdbdbd"));
            itemView.bak.setBackgroundColor(Color.parseColor("#bdbdbd"));
        }


        if(importance.equals("1")) {
            itemView.bak1.setBackgroundColor(Color.parseColor("#2196f3"));
            itemView.bak.setBackgroundColor(Color.parseColor("#ffffff"));
        }
        if(importance.equals("2")) {
            itemView.bak1.setBackgroundColor(Color.parseColor("#ffb300"));
            itemView.bak.setBackgroundColor(Color.parseColor("#ffffff"));
        }

        if(importance.equals("3")) {
            itemView.bak1.setBackgroundColor(Color.parseColor("#f44336"));
            itemView.bak.setBackgroundColor(Color.parseColor("#ffffff"));
        }


        if(listener!=null){
            itemView.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Integer id=itemList.get(i).getId();
                    Server.deleteItem(id,listener);
                }
            });
            itemView.finish.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Integer id=itemList.get(i).getId();
                    Server.finishItem(id,listener);
                }
            });
            itemView.bak.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent=new Intent(context,ItemInfo.class);
                    intent.putExtra("id",String.valueOf(itemList.get(i).getId()));
                    activity.startActivity(intent);
                }
            });

            return view;
        }
        itemView.bak.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(context,ItemInfo.class);
                intent.putExtra("id",String.valueOf(itemList.get(i).getId()));
                fragment.getActivity().startActivity(intent);
            }
        });
        itemView.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Integer id=itemList.get(i).getId();
                Server.deleteItem(id, new StringRequestListener() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(context,response,Toast.LENGTH_SHORT).show();
                        if(who.equals("today"))
                        {
                            TodayFragment today=(TodayFragment)fragment;
                            today.getTodayItem();
                        }else {
                            TodoFragment today=(TodoFragment)fragment;
                            today.getTodayItem();
                        }
                    }

                    @Override
                    public void onError(ANError anError) {
                        Toast.makeText(context,"网络异常",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        itemView.finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Integer id=itemList.get(i).getId();
                Server.finishItem(id, new StringRequestListener() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(context,response,Toast.LENGTH_SHORT).show();
                        if(who.equals("today"))
                        {
                            TodayFragment today=(TodayFragment)fragment;
                            today.getTodayItem();
                        }else {
                            TodoFragment today=(TodoFragment)fragment;
                            today.getTodayItem();
                        }
                    }

                    @Override
                    public void onError(ANError anError) {
                        Toast.makeText(context,"网络异常",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        return view;
    }
}
